﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Photo
{
    public partial class Album : System.Web.UI.Page
    {
        AlbumDO objAlbum = new AlbumDO();

        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!string.IsNullOrEmpty(Request.QueryString["AlbumID"]))
            {

                objAlbum.AlbumID = int.Parse(Request.QueryString["AlbumID"].ToString());
                trHinhAnh.Visible = true;
                if (!IsPostBack) InitForm();
            }
        }
        private void InitForm()
        {
            objAlbum = new AlbumBL().Select(objAlbum);
            txtTenAlbum.Text = objAlbum.Title;
            txtMota.Text = objAlbum.Mota;
            if (!string.IsNullOrEmpty(objAlbum.AnhDaiDien))
            {
                hplImage.NavigateUrl = "~/Media/" + objAlbum.AnhDaiDien;
                imgHinhAnh.ImageUrl = "~/Media/" + objAlbum.AnhDaiDien;
                imgHinhAnh.AlternateText = objAlbum.Title;
                imgHinhAnh.ToolTip = objAlbum.Title;

            }
            txtMota.Text = objAlbum.Mota;
        }
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            xoaanh();
            SetObject();
            if (objAlbum.AlbumID <= 0)
            {
                new AlbumBL().Insert(objAlbum);
                Functions.Alert("Thêm mới thành công", "/Album.aspx");
            }
            else
            {

                new AlbumBL().Update(objAlbum);
                Functions.Alert("Cập nhật thành công", "/ListAlbum.aspx");
            }

        }

        private void xoaanh()
        {
            AlbumDO objab = new AlbumDO();
            if (!string.IsNullOrEmpty(Request.QueryString["AlbumID"]))
            {

                objab = new AlbumBL().Select(new AlbumDO() { AlbumID = Int32.Parse(Request.QueryString["AlbumID"].ToString()) });
            }

            if (!string.IsNullOrEmpty(objab.AnhDaiDien) )
            {
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), objab.AnhDaiDien);
                if (System.IO.File.Exists(SaveLocation))
                {
                    System.IO.File.Delete(SaveLocation);
                }
            }
        }
        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("/ListAlbum.aspx");
        }

        private void SetObject()
        {

            objAlbum.Title = txtTenAlbum.Text;
            objAlbum.Mota = txtMota.Text;
            if (fuImage.HasFile == true)
                objAlbum.AnhDaiDien = UploadFile(fuImage);


        }
        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", fulImage.FileName.Substring(0, fulImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);

                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
    }
}